package com.zcl.service2.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zcl.mapper2.ClassesMapper;
import com.zcl.mapper2.ScoreMapper;
import com.zcl.mapper2.StudentMapper;
import com.zcl.pojo2.Classes;
import com.zcl.pojo2.PageResult;
import com.zcl.pojo2.Student;
import com.zcl.pojo2.score;
import com.zcl.service2.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    //    自动注入两个mapper文件
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectAll();
        //处理班级名称
        for (Student student : students) {
            //处理班级名称
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
            //处理得分
            student.setScore(scoreMapper.selectByPrimaryKey(student.getSid()).getScore());
        }
        return students;
    }

    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        //开始分页
        PageHelper.startPage(page, pageSize);
        //封装查询条件
        Example example = new Example(Student.class);
        //得到所有学生
        List<Student> students = studentMapper.selectByExample(example);
        //处理班级名称
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            String cname = classes.getCname();
            student.setCname(cname);
            score score = scoreMapper.selectByPrimaryKey(student.getSid());
            Integer score1 = score.getScore();
            student.setScore(score1);
        }
        //类型转换
        Page<Student> studentPage = (Page<Student>) students;
        //返回结果
        return new PageResult<>(studentPage.getTotal(), studentPage.getResult(), studentPage.getPages(), page);
    }

    @Override
    public PageResult<Student> search(int page, int pageSize, Student student) {
        //开始分页
        PageHelper.startPage(page, pageSize);
        //封装查询条件
        Example example = new Example(Student.class);
        //根据不同字段查询
        Example.Criteria criteria = example.createCriteria();
        if (student!=null){
            //查询学生姓名
            if (StringUtils.isNoneBlank(student.getSname())){
                criteria.andLike("sname","%"+student.getSname()+"%");
            }
            if (StringUtils.isNoneBlank(student.getAddr())){
                criteria.andLike("addr","%"+student.getAddr()+"%");
            }
            if (student.getCid()!=null&&student.getCid()!=0){
                criteria.andEqualTo("cid",student.getCid());
            }
        }
        //得到所有学生
        List<Student> students = studentMapper.selectByExample(example);
        //处理班级名称
        for (Student st : students) {
            Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
            String cname = classes.getCname();
            st.setCname(cname);
            st.setScore(studentMapper.selectByPrimaryKey(st.getSid()).getScore());
        }
        //类型转换
        Page<Student> studentPage = (Page<Student>) students;
        //返回结果
        return new PageResult<>(studentPage.getTotal(), studentPage.getResult(), studentPage.getPages(), page);
    }

    @Override
    public void insert(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public Student findById(String sid) {

         Student student = studentMapper.selectByPrimaryKey(sid);
         return student;
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public void delete(Student student) {
        studentMapper.deleteByPrimaryKey(student);
    }
}


